package com.qianbao.qianbaobusiness.ui.support.customermanager;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.utils.PhoneNumTools;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.CustomerManagerBean;
import com.qianbao.qianbaobusiness.ui.widget.dialog.ConfirmDialog;

/**
 * @author: Routee
 * @date 2018/6/12
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */

public class CustomerManagerRvAdapter extends BaseRecycleViewAdapter<CustomerManagerBean.ItemsBean> {
    private String checkedMobiles = "";
    private ConfirmDialog mDialog;

    public CustomerManagerRvAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, final CustomerManagerBean.ItemsBean bean) {
        final VH vh = (VH) holder;
        String[] arr = bean.getMerchantName().split("#");
        if (arr.length == 1) {
            vh.mCb.setVisibility(View.GONE);
            vh.mTvNames.setText(bean.getMerchantName());
        } else {
            vh.mCb.setVisibility(View.VISIBLE);
            if (checkedMobiles.contains(bean.getMobile())) {
                vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "\r\n"));
                vh.mTvNames.setMaxLines(Integer.MAX_VALUE);
            } else {
                vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "、"));
                vh.mTvNames.setMaxLines(1);
            }
        }
        vh.mCb.setChecked(checkedMobiles.contains(bean.getMobile()));
        vh.mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkedMobiles = checkedMobiles + bean.getMobile() + "###";
                    vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "\r\n"));
                    vh.mTvNames.setMaxLines(Integer.MAX_VALUE);
                } else {
                    checkedMobiles = checkedMobiles.replaceAll(bean.getMobile() + "###", "");
                    vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "、"));
                    vh.mTvNames.setMaxLines(1);
                }
            }
        });
        vh.mTvManagerName.setText(bean.getBdSaleName());
        vh.mTvContact.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                ConfirmDialog.Builder build = new ConfirmDialog.Builder(mContext);
                String mobile = bean.getMobile();
                if (PhoneNumTools.checkMobile(mobile)) {
                    mobile = mobile.replaceAll("(\\d{3})(\\d+)(\\d{4})", "$1 $2 $3");
                }
                mDialog = build.setContentText(mobile)
                                  .setPositiveText("拨打")
                                  .setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                                      @Override
                                      public void click(boolean b) {
                                          mDialog.dismiss();
                                          if (b) {
                                              Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + bean.getMobile()));
                                              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                              mContext.startActivity(intent);
                                          }
                                      }
                                  })
                                  .create();
                mDialog.show();
            }
        });
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_customer_manager, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class VH extends RecyclerView.ViewHolder {

        TextView mTvNames;
        CheckBox mCb;
        TextView mTvManagerName;
        TextView mTvManagerState;
        TextView mTvContact;

        public VH(View itemView) {
            super(itemView);
            mTvNames = itemView.findViewById(R.id.tv_names);
            mCb = itemView.findViewById(R.id.cb);
            mTvManagerName = itemView.findViewById(R.id.tv_manager_name);
            mTvManagerState = itemView.findViewById(R.id.tv_manager_state);
            mTvContact = itemView.findViewById(R.id.tv_contact);
        }
    }
}
